﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MailQueueProcessingService
{
    class Settings
    {
        private static string _SmtpServer;
        private static string _SmtpUser;
        private static string _SmtpPass;
        private static int _SmtpPort;
        private static bool _SmtpSsl;
        private static string _SmtpOnBehalf;
        private static string _SmtpDummy;
        private static bool _Pause;
        private static int _MaxBacthSize;
        private static int _SendInterval;
        private static int _Retries;

        public static string SmtpServer { get { return _SmtpServer; } set { _SmtpServer = value; } }
        public static string SmtpUser { get { return _SmtpUser; } set { _SmtpUser = value; } }
        public static string SmtpPass { get { return _SmtpPass; } set { _SmtpPass = value; } }
        public static int SmtpPort { get { return _SmtpPort; } set { _SmtpPort = value; } }
        public static bool SmtpSsl { get { return _SmtpSsl; } set { _SmtpSsl = value; } }
        public static string SmtpOnBehalf { get { return _SmtpOnBehalf; } set { _SmtpOnBehalf = value; } }
        public static string SmtpDummy { get { return _SmtpDummy; } set { _SmtpDummy = value; } }
        public static bool Pause { get { return _Pause; } set { _Pause = value; } }
        public static int MaxBacthSize { get { return _MaxBacthSize; } set { _MaxBacthSize = value; } }
        public static int SendInterval { get { return _SendInterval * 1000; } set { _SendInterval = value; } }
        public static int Retries { get { return _Retries; } set { _Retries = value; } }

        public static void Load(string connString)
        {
            //System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
            //string connString = ((string)(configurationAppSettings.GetValue("connectionString", typeof(string))));

            string[] Fields = new string[] { "smtpserver", "smtpuser", "smtppass", "smtpport", "smtpssl", "smtponbehalf", "smtpdummy", "pause", "maxbactchsize", "sendinterval", "retries" };

            string sqlCmdText = "SELECT [smtpserver],[smtpuser],[smtppass],[smtpport],[smtpssl],[smtponbehalf],[smtpdummy],[pause],[maxbactchsize],[sendinterval],[retries] " +
                " FROM [Settings] WHERE [iscurrentsetting] = 1";

            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
            conn.Open();
            System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = sqlCmdText;
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
            {
                for (int i=0; i<Fields.Length; i++)
                {
                    if (!dr.IsDBNull(i))
                    {
                        string s = Fields[i];
                        switch (s)
                        {
                            case "smtpserver":
                                _SmtpServer = (string)dr[s];
                                break;
                            case "smtpuser":
                                _SmtpUser = (string)dr[s];
                                break;
                            case "smtppass":
                                _SmtpPass = (string)dr[s];
                                break;
                            case "smtpport":
                                _SmtpPort = (int)dr[s];
                                break;
                            case "smtpssl":
                                _SmtpSsl = (bool)dr[s];
                                break;
                            case "smtponbehalf":
                                _SmtpOnBehalf = (string)dr[s];
                                break;
                            case "smtpdummy":
                                _SmtpDummy = (string)dr[s];
                                break;
                            case "pause":
                                _Pause = (bool)dr[s];
                                break;
                            case "maxbactchsize":
                                _MaxBacthSize = (int)dr[s];
                                break;
                            case "sendinterval":
                                _SendInterval = (int)dr[s];
                                break;
                            case "retries":
                                _Retries = (int)dr[s];
                                break;
                        }
                    }
                }
            }

            conn.Close();
        }

    }
}
